﻿using CommunityToolkit.Mvvm.ComponentModel;
using MeasureAbstract.Base;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace MeasureAbstract.IMotionDevice
{
    [INotifyPropertyChanged]
    public abstract partial class IMotionDevice : IDeviceBase
    {
        public abstract string Name { get; }
        public abstract string Vender { get; }

        public abstract string Model { get; }
        public abstract string Description { get; }
        public bool _ConnectState;
        public bool ConnectState
        {
            get
            {
                return _ConnectState;
            }
            set
            {
                SetProperty(ref _ConnectState, value);
            }
        }

        public bool _MalfunctionState;
        public bool MalfunctionState
        {
            get
            {
                return _MalfunctionState;
            }
            set
            {
                SetProperty(ref _MalfunctionState, value);
            }
        }

        public abstract void Connect();
        public abstract void DisConnect();
        public abstract void ReSet();



        /// <summary>
        /// 设备参数
        /// </summary>
        public abstract MotionDeviceConfig? Config { get; set; }


        /// <summary>
        /// 运动
        /// </summary>
        /// <param name="AxisID">轴号</param>
        /// <param name="Position">位置</param>
        /// <param name="Speed">速度</param>
        /// <param name="Acc">加速度</param>
        /// <param name="Dcc">减速度</param>
        /// <param name="AbsoluteMovement">绝对运动还是相对运动</param>
        /// <returns></returns>
        public abstract int Run(int AxisID, double Position, double Speed, double Acc, double Dcc, bool AbsoluteMovement = true);

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="AxisID">轴号</param>
        /// <returns></returns>
        public abstract int Stop(int AxisID);

        /// <summary>
        /// 开使能
        /// </summary>
        /// <param name="AxisID">轴号</param>
        /// <returns></returns>
        public abstract int Enable(int AxisID);

        /// <summary>
        /// 关使能
        /// </summary>
        /// <param name="AxisID">轴号</param>
        /// <returns></returns>
        public abstract int Disable(int AxisID);

        /// <summary>
        /// 是否达位
        /// </summary>
        /// <param name="AxisID"></param>
        /// <returns></returns>
        public abstract bool ReachedState(int AxisID);



        public abstract int TriggerOpen(int AxisID,double StartPosition,double EndPosition,double Interval,int PuslseWidth_us);

        public abstract int TriggerClose(int AxisID);
        public abstract int TriggerOne(int AxisID);

        public virtual void HeartBeat(ref object ResponseFlag,int MaxResponseTime_ms)
        {
           
        }

    }

    public partial class Axis : ObservableObject
    {
        [ObservableProperty]
        [JsonIgnore]
        private bool selected;

        [ObservableProperty]
        [JsonIgnore]
        private string? axisName;

        [ObservableProperty]
        [JsonIgnore]
        private int axisID;

        [ObservableProperty]
        [JsonIgnore]
        private bool enableState;

        [ObservableProperty]
        [JsonIgnore]
        private double currentPosition;

        [ObservableProperty]
        [JsonIgnore]
        private double currentSpeed;

        [ObservableProperty]
        [JsonIgnore]
        private double setStartPosition;

        [ObservableProperty]
        [JsonIgnore]
        private double setEndPosition;

        [ObservableProperty]
        [JsonIgnore]
        private double setStepValue;

        [ObservableProperty]
        [JsonIgnore]
        private double setSpeed;

        [ObservableProperty]
        [JsonIgnore]
        private bool stepMove;

        [ObservableProperty]
        [JsonIgnore]
        private bool supportPulseOut;

        [ObservableProperty]
        [JsonIgnore]
        private bool enablePulseOutMode;

        [ObservableProperty]
        [JsonIgnore]
        private double pulseTriggerStartPosition;

        [ObservableProperty]
        [JsonIgnore]
        private double pulseTriggerEndPosition;

        [ObservableProperty]
        [JsonIgnore]
        private double pulseTriggerInterval;

        public double Accuracy { get; set; }

        [JsonIgnore]
        public bool IsReached { get; set; } = false;
    }




    public partial class CommunicationMode : ObservableObject
    {
        [ObservableProperty]
        [JsonIgnore]
        private bool support = false;
        [ObservableProperty]
        [JsonIgnore]
        private bool selected = false;
        [ObservableProperty]
        [JsonIgnore]
        private string name = "";
    }


    public partial class CommunicationConfig : ObservableObject
    {
        //通信参数配置
        [ObservableProperty]
        [JsonIgnore]
        //private ObservableCollection<CommunicationMode> communicationList = new ObservableCollection<CommunicationMode>()
        //{
        //    new CommunicationMode() {Name="TCP",Selected=true,Support=true },
        //    new CommunicationMode() {Name="UDP",Selected=false,Support=true},
        //    new CommunicationMode() {Name="UART",Selected=false,Support=true},
        //    new CommunicationMode() {Name="GPIB",Selected=false,Support=false}
        //};
        private ObservableCollection<CommunicationMode> communicationList;


        [ObservableProperty]
        [JsonIgnore]
        private bool showTCP = true;
        [ObservableProperty]
        [JsonIgnore]
        private bool showUDP;
        [ObservableProperty]
        [JsonIgnore]
        private bool showUART;
        [ObservableProperty]
        [JsonIgnore]
        private bool showGPIB;
        [ObservableProperty]
        [JsonIgnore]

        private string? localIP = "192.168.0.100";
        [ObservableProperty]
        [JsonIgnore]
        private string? localPort = "8080";
        [ObservableProperty]
        [JsonIgnore]
        private string? destIP = "192.168.0.200";
        [ObservableProperty]
        [JsonIgnore]
        private string? destPort = "8080";
        [ObservableProperty]
        [JsonIgnore]
        private string? portName = "COM1";
        [ObservableProperty]
        [JsonIgnore]
        private string? bandRate = "115200";
        [ObservableProperty]
        [JsonIgnore]
        private string? gpibAddr = "5";
    }

    public sealed partial class MotionDeviceConfig : CommunicationConfig
    {
        //转轴相关
        [ObservableProperty]
        [JsonIgnore]
        private ObservableCollection<Axis>? axes;
    }



}
